package com.fairytask.ui
{
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.MoneyDefine;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.MoneyTools;
	import com.fairytask.ui.base.FrameUI;
	
	import flash.events.MouseEvent;
	
	/**
	 * 商店界面中的商品信息 
	 * @author pangj
	 * 
	 */
	public class ShopGoodsUI extends FrameUI
	{

		public var goodsImg      :Clip;               //商品图标
		public var numClip       :Clip;               //剩余次数 文字
		public var goodNum       :Label;              //商品数量
		public var nameLab       :Label;              //商品名称
		public var priceLab      :Label;              //商品价格
		public var BuyBtn        :Button;             //购买按钮
		
		public var good          :Object;             //需要购买的商品
		public var _haveMoney    :int;                //玩家拥有的金钱
		
		private var _configID    :int;                //商品id    
		private var isArena      :Boolean             //是否是竞技场商店
		
		public function ShopGoodsUI(good:Object,haveMoney:int,isArena:Boolean=false)
		{
			this.good=good;
			this._haveMoney=haveMoney;
			this.isArena = isArena;
			this._configID = good.id;
			initEvent();
			init();
		}
		
		/**
		 * 初始化，设置商品信息 
		 * 
		 */
		private function init():void
		{
			setGoodsImg(good.icon);
			setNameLab(good.id);
			setPriceLab(good.price,good.selltype);
			setNumLab();
			//取消窗口拖动
			windowBG.removeDrag();
		}
		private function initEvent():void
		{
			BuyBtn.addEventListener(MouseEvent.CLICK,onBuyGood)
		}
		/**
		 * 显示商品图片
		 * 
		 */
		public function setGoodsImg(value:int):void 
		{
			goodsImg.source=ResConfig.iconPath + value + ResConfig.iconImg;
			goodsImg.addEventListener(MouseEvent.ROLL_OVER,function(mevt:MouseEvent):void
			{
				TipsManage.ShowItemTips(_configID,true);
			});
			goodsImg.addEventListener(MouseEvent.ROLL_OUT,function(mevt:MouseEvent):void
			{
				ToolTips.hide();
			});
		}
		/**
		 * 显示商品名称 
		 * 
		 */
		public function setNameLab(name:String):void
		{
			nameLab.htmlStr = StaticIni.getSwfLangStr(name);
		}
		/**
		 * 显示商品价格 
		 * 
		 */
		public function setPriceLab(price:String,selltype:String):void
		{
			priceColor(_haveMoney,int(price));
			var showPrice :String = MoneyTools.setMoney(int(price));
			switch(int(selltype))
			{
				case MoneyDefine.CAPITAL_TYPE_SILVER:
				{
					priceLab.htmlStr = showPrice + " "+StaticIni.getSwfLangStr("1000000067");//银两
					break;
				}
				case MoneyDefine.CAPITAL_TYPE_BINDSILVER:
				{
					priceLab.htmlStr = showPrice + " "+StaticIni.getSwfLangStr("1000000113");//绑定银
					break;
				}
				case MoneyDefine.CAPITAL_TYPE_GOLD:
				{
					priceLab.htmlStr = showPrice + " "+StaticIni.getSwfLangStr("1000000066");//仙玉
					break;
				}
				case MoneyDefine.CAPITAL_TYPE_BINDGOLD:
				{
					priceLab.htmlStr = showPrice + " "+StaticIni.getSwfLangStr("1000000114");//点券
					break;
				}
				case MoneyDefine.CAPITAL_TYPE_SCORE:
				{
					priceLab.htmlStr = showPrice + " "+StaticIni.getSwfLangStr("1000000115");//积分
					break;
				}
				case MoneyDefine.CAPITAL_TYPE_CONTRIBUTE:
				{
					priceLab.htmlStr = showPrice + " "+StaticIni.getSwfLangStr("1000000116");//贡献
					break;
				}
				case MoneyDefine.CAPITAL_TYPE_SHELL:
				{
					priceLab.htmlStr = showPrice + " "+StaticIni.getSwfLangStr("1000000117");//贝壳
					break;
				}
				case MoneyDefine.CAPITAL_TYPE_SPIRIT:
				{
					priceLab.htmlStr = showPrice + " "+StaticIni.getSwfLangStr("1000000110");//灵气
					break;
				}
				case MoneyDefine.CAPITAL_TYPE_GUILDCONTRIBUTE:
				{
					priceLab.htmlStr = showPrice + " "+StaticIni.getSwfLangStr("1000000118");//帮贡
					break;
				}
			}
		}
		/**
		 *设置价格显示颜色 
		 * @param haveMoney 玩家拥有的金钱
		 * @param price     商品的价格
		 * 
		 */
		public function priceColor(haveMoney:int,price:int):void
		{
			priceLab.color = haveMoney<int(price)?"D71345":"FFCB4A";
		}

		/**
		 * 显示商品剩余数量
		 * 
		 */
		public function setNumLab():void
		{	
			if(good.SurplusNum==0)
			{
				goodNum.htmlStr = "∞";
				return ;
			}
			goodNum.htmlStr = String(good.SurplusNum);
		}
		/**
		 * 玩家点击购买按钮
		 * 
		 */
		private function onBuyGood(e:MouseEvent):void
		{
			if(good.amount==0 || _haveMoney<good.price || isArena)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SHOPPING_ITEM_BUY,good.id,1);
			}
			else
			{
				ShopFrameUI.getInstance().setAmount(good);
			}
		}
		/**
		 *设置玩家拥有的金钱 
		 * @param val
		 * 
		 */
		public function set haveMoney(val:int):void
		{
			_haveMoney = val;
			priceColor(_haveMoney,good.price);
		}
	}
}